package io.wdb.modules.generator.generator.controller.client;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import io.wdb.common.annotation.SysLog;
import io.wdb.common.utils.PageUtils;
import io.wdb.common.utils.Query;
import io.wdb.common.utils.R;
import io.wdb.modules.generator.generator.controller.api.BaseController;
import io.wdb.modules.generator.generator.entity.customer.AccountCreditLogEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.BookEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.BookMaintainLogEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.OrderTransEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.UserInfoEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.UserPointLogEntityCustomer;
import io.wdb.modules.generator.generator.entity.web.BaseResponse;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.client.ClientManagerService;

@Controller
@RequestMapping("/client/manager")
public class ClientManagerController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(ClientManagerController.class);
	@Autowired
	private ClientManagerService clientManagerService;
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("clientmanager:list")
	//@RequiresPermissions("sys:generator:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<UserInfoEntityCustomer> list = clientManagerService.queryList(query);
		PageInfo<UserInfoEntityCustomer> page = new PageInfo<UserInfoEntityCustomer>(list);
		int total = clientManagerService.queryListTotal(query);
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 修改
	 */
	@SysLog("用户类型，用户积分，用户信用分改变")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("clientmanager:updatepoint")
	public BaseResponse update( @RequestBody UserInfoEntityCustomer userInfoEntityCustomer){
		BaseResponse baseResponse = success();
		try {
			//修改用户相关信息，比如用户类型
			if(userInfoEntityCustomer.getUserType()!=null||userInfoEntityCustomer.getStatus()!=null) {
				clientManagerService.updateUserInfo(userInfoEntityCustomer);
			}
			//更改用户积分
			if(userInfoEntityCustomer.getPointValue()!=null) {
				clientManagerService.updatePointValue(userInfoEntityCustomer);//更改积分
			}
			//更改用户信用分
			if(userInfoEntityCustomer.getCreditValue()!=null) {
				clientManagerService.updateCreditValue(userInfoEntityCustomer);//更改信用分
			}
		}catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:insertUser
	 * @Description:添加用户
	 * @Time 2017年12月18日 下午1:49:09
	 * @author: Tom
	 * @param userInfoEntityCustomer
	 * @return
	 */
	@SysLog("新增用户")
	@ResponseBody
	@RequestMapping("/insert")
	@RequiresPermissions("clientmanager:save")
	public BaseResponse insertUser(@RequestBody UserInfoEntityCustomer userInfoEntityCustomer){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = clientManagerService.insertUser(userInfoEntityCustomer);
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse=failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:mainTain
	 * @Description:客户报修详细
	 * @Time 2018年1月10日 上午11:26:18
	 * @author: Tom
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/maintain")
	@RequiresPermissions("clientmanager:list")
	public R mainTain(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BookMaintainLogEntityCustomer> list = clientManagerService.queryMainTainListByUserId(query);
		int total = clientManagerService.queryMainTainListTotal(query);
		
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:getCurrentBorrowBooks
	 * @Description:查询用户借阅图书
	 * @Time 2018年1月8日 上午9:42:02
	 * @author: Tom
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getBorrowBooks")
	@RequiresPermissions("clientmanager:list")
	public R getCurrentBorrowBooks(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<OrderTransEntityCustomer> list = clientManagerService.queryCurrentBorrowBooks(query);
		PageInfo<OrderTransEntityCustomer> page = new PageInfo<OrderTransEntityCustomer>(list);
		int total = (int) page.getTotal();

		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:getPointRecord
	 * @Description:用户积分流水记录
	 * @Time 2018年1月9日 下午3:15:06
	 * @author: Tom
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getPointRecord")
	@RequiresPermissions("clientmanager:list")
	public R getPointRecord(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<UserPointLogEntityCustomer> list = clientManagerService.queryPointRecord(query);
		PageInfo<UserPointLogEntityCustomer> page = new PageInfo<UserPointLogEntityCustomer>(list);
		int total = (int) page.getTotal();

		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:getCreditRecord
	 * @Description:用户信用分流水
	 * @Time 2018年1月9日 下午4:10:04
	 * @author: Tom
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getCreditRecord")
	@RequiresPermissions("clientmanager:list")
	public R getCreditRecord(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<AccountCreditLogEntityCustomer> list = clientManagerService.queryCreditRecord(query);
		PageInfo<AccountCreditLogEntityCustomer> page = new PageInfo<AccountCreditLogEntityCustomer>(list);
		int total = (int) page.getTotal();

		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:cancelBook
	 * @Description:书籍作废操作
	 * @Time 2018年1月9日 下午7:36:23
	 * @author: Tom
	 * @param bookEntityEntity
	 * @return
	 */
	@SysLog("用户借阅书籍作废")
	@ResponseBody
	@RequestMapping("/cancelBook")
	@RequiresPermissions("clientmanager:cancelbook")
	public BaseResponse cancelBook(@RequestBody BookEntityCustomer bookEntity){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = clientManagerService.cancelBook(bookEntity);
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse = failed(e);
			logger.error(e.getMessage(),e);
		}
		return baseResponse;
	}
}
